﻿using System;
using System.Collections;
using System.Xml;
using System.Web;
using System.IO;
using ComLib.ImageLib;


namespace SwfupLoadDemo.Common
{
    /// <summary>
    /// uploadnewspic 的摘要说明
    /// </summary>
    public class uploadnewspic : IHttpHandler
    {

        public void ProcessRequest(HttpContext context)
        {           
            if (context.Request.QueryString["job"] != null)
            {
                string strJob = context.Request.QueryString["job"].ToString().ToLower();
                try
                {
                    if (strJob.Equals("add"))
                    {
                        HttpPostedFile file;

                        // 文件名
                        string upFileName = string.Empty;
                        string fileId = string.Format("{0:yyyyMMddHHmmssffff}", DateTime.Now);
                       
                        //获得图片存储目录
	                    var strFolder = DateTime.Now.ToString("yyyyMMdd");
						//var virthPath = string.Format("/upload/user/{0}/{1}/", SysUserService.Instance.CurrentUserID, strFolder);
						//var virthThumbPath = string.Format("/upload/user/{0}/ThumbPhoto/{1}/", SysUserService.Instance.CurrentUserID, strFolder);
						var virthPath = string.Format("/upload/user/{0}/{1}/", "xx", strFolder);
						var virthThumbPath = string.Format("/upload/user/{0}/ThumbPhoto/{1}/", "xxx", strFolder);
	                    var upFileNameStr = string.Empty;
						string dir = context.Server.MapPath(virthPath);
						var thumbDir = context.Server.MapPath(virthThumbPath);

						if (!System.IO.Directory.Exists(dir)) {
							Directory.CreateDirectory(dir);
						}
						if (!System.IO.Directory.Exists(thumbDir)) {
							Directory.CreateDirectory(thumbDir);
						}
                        for (int i = 0; i < context.Request.Files.Count; ++i)
                        {
                            file = context.Request.Files[i];
                            if (file == null || file.ContentLength == 0 || string.IsNullOrEmpty(file.FileName)) continue;

                            string fileExtension = new FileInfo(file.FileName).Extension.ToLower();

                            /// 将上传文件以不重复文件名形式保存到本站目录下
							upFileName += dir  + fileId + fileExtension;
							upFileNameStr += virthThumbPath + fileId + fileExtension;
                            file.SaveAs(upFileName);
							var thumbPhotoPath = thumbDir + fileId + fileExtension;


							MakeThumbImage.MakeThumbPhoto(upFileName, thumbPhotoPath,
	                                                      320, 160, MakeThumbImage.ThumbPhotoModel.HW);
                        }
                        context.Response.StatusCode = 200;
						context.Response.Write(upFileNameStr);
                    }
                    if (strJob.Equals("del"))
                    {
                        string fileName = context.Request.QueryString["id"].ToString(); 
                        File.Delete(context.Request.MapPath(fileName));
						File.Delete(context.Request.MapPath(fileName.Replace("ThumbPhoto/","")));
                        context.Response.StatusCode = 200;
                    }
                }
                catch (Exception ex)
                {
                    context.Response.StatusCode = 500;
                    context.Response.Write(ex.Message);
                    context.Response.End();
                }
                finally
                {
                    context.Response.End();
                }
            }
        }

        public bool IsReusable
        {
            get
            {
                return false;
            }
        }

        private bool ThumbnailCallback()
        {
            return false;
        }
    }
}